package com.example.demo3.utils.mpgene_oracle;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.example.demo3.utils.constant.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * mybatis-plus代码生成》oracle数据库》eam数据库。
 *
 * @author cjw 2020-03-23
 */
public class MpGeneOracle_eam {
	
	/**
	 * 项目路径。
	 */
	public static String projectPath = System.getProperty("user.dir");
	
	
	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		StringBuilder help = new StringBuilder();
		help.append("请输入" + tip + "：");
		System.out.println(help.toString());
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotEmpty(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "！");
	}
	
	/**
	 * @param
	 * @return
	 * @author cjw 2020-03-26
	 */
	public static void main(String[] args) {
		String author = "xsyuan";
		String tablePrefix = "sys_";
		generateCode(author, tablePrefix);
	}
	
	/**
	 * @param
	 * @return
	 * @author cjw 2020-03-26
	 */
	public static void generateCode(String author, String tablePrefix) {
		/**
		 * 代码生成器
		 */
		AutoGenerator mpg = new AutoGenerator();
		
		/**
		 * 全局配置
		 */
		GlobalConfig gc = new GlobalConfig();
		gc.setOutputDir(projectPath + "/src/main/java");
		gc.setAuthor(author);
		gc.setOpen(false);
		// gc.setSwagger2(true); 实体属性 Swagger2 注解
		mpg.setGlobalConfig(gc);
		
		/**
		 * 数据源配置
		 */
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl("jdbc:oracle:thin:@10.10.5.122/EAMTEST");
		dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
		dsc.setUsername("EAM");
		dsc.setPassword("EAM");
		mpg.setDataSource(dsc);
		
		/**
		 * 包配置
		 */
		PackageConfig pc = new PackageConfig();
		pc.setModuleName(scanner("模块名"));
		pc.setParent("com.example.demo3.utils.mpgene_oracle");
		mpg.setPackageInfo(pc);
		System.out.println("pc.getModuleName()=" + pc.getModuleName());
		System.out.println("pc.getParent()=" + pc.getParent());
		
		/**
		 * 自定义配置
		 */
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				// to do nothing
			}
		};
		
		/**
		 * 自定义输出配置
		 */
		List<FileOutConfig> focList = new ArrayList<>();
		// 自定义配置会被优先输出
		focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				// 自定义输出文件名称
				//return projectPath + "/mybatis-plus-sample-generator/src/main/resources/mapper/"
				//		+ pc.getModuleName().replace(".", "/") + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
				
				
				String str = projectPath + "/src/main/java/"
						+ pc.getParent().replace(".", "/")
						+ "/mapping/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
				
				System.out.println("mapping path=" + str);
				return str;
			}
		});
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);
		
		/**
		 * 配置模板
		 */
		mpg.setTemplate(new TemplateConfig().setXml(null));
		
		/**
		 * 策略配置
		 */
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		//strategy.setSuperEntityClass("");
		strategy.setEntityLombokModel(true);
		// 写于父类中的公共字段
		strategy.setSuperEntityColumns();
		strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
		
		// 公共controller父类
		strategy.setRestControllerStyle(true);
		strategy.setSuperControllerClass("com.example.demo3.controller.BaseController");
		strategy.setControllerMappingHyphenStyle(true);
		
		/**
		 * 设置表前缀信息。
		 * changed by cjw
		 */
		//strategy.setTablePrefix(pc.getModuleName() + "_");
		strategy.setTablePrefix(tablePrefix);//m_t_
		mpg.setStrategy(strategy);
		// 选择 freemarker 引擎需要指定如下，注意 pom 依赖必须有！
		mpg.setTemplateEngine(new FreemarkerTemplateEngine());
		mpg.execute();
	}
	
	
}
